﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class PlanTypeDao
    {
        public bool Delete(SqlConnection conn, int typeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("DECLARE @RetValue bit ");
            builder.Append("IF NOT EXISTS(SELECT [PlanId] FROM [dbo].[Plan_Work] WHERE [TypeId] = @TypeId) ");
            builder.Append("BEGIN ");
            builder.Append("\tDELETE FROM [dbo].[Plan_Type] WHERE [TypeId] = @TypeId ");
            builder.Append("\tSET @RetValue = 1 ");
            builder.Append("END ");
            builder.Append("ELSE ");
            builder.Append("\tSET @RetValue = 0 ");
            builder.Append("SELECT @RetValue AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@TypeId", SqlDbType.Int) };
            commandParameters[0].Value = typeId;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            return ((obj2 != null) && Convert.ToBoolean(obj2));
        }

        public int Insert(SqlConnection conn, PlanTypeData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("INSERT INTO [dbo].[Plan_Type] ");
            builder.Append("( ");
            builder.Append("\t[TypeName], ");
            builder.Append("\t[TypeNo] ");
            builder.Append(") ");
            builder.Append("VALUES ");
            builder.Append("( ");
            builder.Append("\t@TypeName, ");
            builder.Append("\t@TypeNo ");
            builder.Append(") ");
            builder.Append("SELECT SCOPE_IDENTITY() AS RetValue ");
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@TypeName", SqlDbType.NVarChar, 200);
            commandParameters[0].Value = data.TypeName;
            commandParameters[1] = new SqlParameter("@TypeNo", SqlDbType.Int);
            commandParameters[1].Value = data.TypeNo;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, builder.ToString(), commandParameters);
            if (obj2 != null)
            {
                return Convert.ToInt32(obj2);
            }
            return 0;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[TypeId], ");
            builder.Append("\t[TypeName], ");
            builder.Append("\t[TypeNo] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Plan_Type] ");
            builder.Append("ORDER BY ");
            builder.Append("\t[TypeNo] ASC ");
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString());
            set.Tables[0].TableName = "Plan_Type";
            return set.Tables[0];
        }

        public DataTable SelectByTypeId(SqlConnection conn, int typeId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT ");
            builder.Append("\t[TypeId], ");
            builder.Append("\t[TypeName], ");
            builder.Append("\t[TypeNo] ");
            builder.Append("FROM ");
            builder.Append("\t[dbo].[Plan_Type] ");
            builder.Append("WHERE ");
            builder.Append("\t[TypeId] = @TypeId ");
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@TypeId", SqlDbType.Int) };
            commandParameters[0].Value = typeId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, builder.ToString(), commandParameters);
            set.Tables[0].TableName = "Plan_Type";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, PlanTypeData data)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SET NOCOUNT ON ");
            builder.Append("UPDATE [dbo].[Plan_Type] ");
            builder.Append("SET ");
            builder.Append("\t[TypeName] = @TypeName, ");
            builder.Append("\t[TypeNo] = @TypeNo ");
            builder.Append("WHERE ");
            builder.Append("\t[TypeId] = @TypeId ");
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@TypeId", SqlDbType.Int);
            commandParameters[0].Value = data.TypeId;
            commandParameters[1] = new SqlParameter("@TypeName", SqlDbType.NVarChar, 200);
            commandParameters[1].Value = data.TypeName;
            commandParameters[2] = new SqlParameter("@TypeNo", SqlDbType.Int);
            commandParameters[2].Value = data.TypeNo;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, builder.ToString(), commandParameters);
            return true;
        }
    }
}

